<template>
  <div class="printForm">
    <div class="area-header">
      <span class="area-header-title">{{ $t('申请单') }}</span>
    </div>
    <table class="printTable">
      <tr>
        <td class="tit">{{ $t('申请标题') }}</td>
        <td colspan="3">{{ form.actTitle }}</td>
      </tr>
      <tr>
        <td class="tit">{{ $t('合伙企业') }}</td>
        <td>{{ form.hhqyName }}</td>

        <td class="tit">{{ $t('项目') }}</td>
        <td>
          <MainBodySelect
            v-model="form.dealId"
            parameters="deal"
          />
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('预计投资时间') }}</td>
        <td>
          {{ parseTime(form.dateInvestmentEstimate) }}
        </td>
        <td class="tit">{{ $t('预计投资金额') }}{{ $t('金额单位') }}</td>
        <td>
          {{ moneyFormat(form.amountInvestmentEstimate, 'w') }}
        </td>
      </tr>
      <tr>
        <td class="tit">{{ $t('备注') }}</td>
        <td colspan="3">
          {{ form.remark }}
        </td>
      </tr>
    </table>
  </div>
</template>

<script>
    import { saveHhqyOccupy, getHhqyOccupy } from '@/api/hhqy/occupy/hhqyOccupy'
    import MainBodySelect from '@/components/MainBodySelect/index'
    import { findNotExistsOccupy } from '@/api/hhqy/base/hhqyBase'
    export default {
      components: {
        MainBodySelect
      },
        data() {
            return {
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                  hhqyId: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], dealId: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], dateInvestmentEstimate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], amountInvestmentEstimate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], actTitle: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ]
                },
                // 遮罩层
                loading: false,
                // 操作按钮默认不展示
                buttonShow: true,
                // 未被项目占用的合伙企业
                hhqyBaseOptions: []
            }
        },
        created() {
            this.$bus.$emit('async_done')
            findNotExistsOccupy().then(response => {
              this.hhqyBaseOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data) {
                if (data != null && data.id !== undefined) {
                    getHhqyOccupy(data.id).then(response => {
                        this.form = response.data
                    })
                } else {
                    this.reset()
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                hhqyId: undefined,
                dealId: undefined,
                dateInvestmentEstimate: undefined,
                amountInvestmentEstimate: undefined,
                remark: undefined,
                createBy: undefined,
                updateBy: undefined,
                createTime: undefined,
                updateTime: undefined,
                delFlag: undefined,
                actId: undefined,
                actStatus: '0',
                actTitle: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function(type) {
                this.$refs['form'].validate(valid => {
                    // 验证数据
                    if (valid) {
                        // 弹出遮罩层
                        this.loading = true
                        // 保存数据
                        saveHhqyOccupy(this.form).then(response => {
                            if (response.code === 200) {
                                // 点击的按钮是保存并启动流程
                                if (type === 'process') {
                                    // 启动流程
                                    this.startProcess(response.data.id)
                                // 点击的仅保存
                                } else {
                                    this.msgSuccess(this.$t('保存成功'))
                                    // 关闭form页面
                                    this.closeForm()
                                }
                            } else {
                                this.msgError(response.msg)
                                this.loading = false
                            }
                        })
                    }
                })
           },
            // 启动流程
           startProcess: function(dataId) {
               this.loading = false
               // 启动流程时候传入的参数
               const processData = {}
               // 待办标题
               processData.title = this.form.actTitle
               // 业务表名
               processData.dataTable = 'hhqy_occupy'
               // 业务ID
               processData.dataId = dataId
               // 业务表单地址(待办页面需要打开的页面)
               processData.formPath = '@/views/hhqy/occupy/hhqyOccupyForm'
               processData.viewPath = '@/views/hhqy/occupy/hhqyOccupyView'
               // 菜单标识(根据菜单标识判断的走哪个流程)
               processData.menuKey = 'hhqy_occupy'
               this.startProcessByMenu(processData, () => {
                   this.form.actStatus = 'D'
                   this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
                   this.closeForm()
               })
           }

        }

    }
</script>
